package sy.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.SysBuildingDaoI;
import sy.model.SysBuilding;

@Repository("sysBuildingDao")
public class SysBuildingDaoImpl implements SysBuildingDaoI {
	
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}
	
	@Override
	public Serializable save(SysBuilding sysBuilding) {
		return getCurrentSession().save(sysBuilding);//直接用
		
	}

	@Override
	public void remove(SysBuilding sysBuilding) {
		// TODO Auto-generated method stub
		getCurrentSession().delete(sysBuilding);
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysBuildingDaoI#update(sy.model.SysBuilding)
	 */
	@Override
	public void update(SysBuilding sysBuilding) {
		// TODO Auto-generated method stub
		getCurrentSession().update(sysBuilding);
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysBuildingDaoI#list()
	 */
	@Override
	public List<SysBuilding> list() {
		// TODO Auto-generated method stub
		return getCurrentSession().createQuery("from SysBuilding").list();
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysBuildingDaoI#getSysBuildingById(java.lang.String)
	 */
	@Override
	public SysBuilding getSysBuildingById(String id) {
		// TODO Auto-generated method stub
		String hql = "from SysBuilding where id = :id";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("id", id);
		List<SysBuilding> list = query.list();
		if (list.size()>0){
			return list.get(0);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysBuildingDaoI#listSysBuildingByKey(java.lang.String)
	 */
	@Override
	public List<SysBuilding> listSysBuildingByKey(String key) {
		// TODO Auto-generated method stub
		String hql = "from SysBuilding where code like :key or name like :key or address like :key or sysProject.id like :key";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("key", "%"+key+"%");
		List<SysBuilding> list = query.list();
		return list;
	}
	
	@Override
	public List<SysBuilding> getBuildingByProject(String projectId) {
		// TODO Auto-generated method stub
		String hql = "from SysBuilding where  sysProject.id = :projectId";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("projectId", projectId);
		List<SysBuilding> list = query.list();
		return list;
	}

}
